package com.newbsd.web.maventest.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.newbsd.web.maventest.common.pojo.EasyUIDataGridJSON;
import com.newbsd.web.maventest.dao.impl.UserDaoImpl;
import com.newbsd.web.maventest.mvc.InnerDispatcherController;
import com.newbsd.web.maventest.pojo.Ticket;
import com.newbsd.web.maventest.pojo.User;
import com.newbsd.web.maventest.service.UserQueryDao;
import com.newbsd.web.maventest.service.impl.UserQueryDaoImpl;
import com.newbsd.web.maventest.util.Md5Utils;

/**
 * @author wangjing
 *
 */
public class UserController extends InnerDispatcherController{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	//对用户查询到的所有票进行分页
	public void pageDatagrid(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		
		String origin = request.getParameter("origin");
		String destination = request.getParameter("destination");
		Integer page = Integer.valueOf(request.getParameter("page"));
		Integer rows = Integer.valueOf(request.getParameter("rows"));
		
		UserQueryDao uqd = new UserQueryDaoImpl();
		List<Ticket> ticketlist = uqd.QueryTickets(origin, destination,page,rows);

		//int totalRecords = uqd.QueryTicNum(origin, destination);
		
		request.setAttribute("ticketList", ticketlist);
		request.getRequestDispatcher("WEB-INF/view/utickets.jsp").forward(request,
				response);
		
		
		EasyUIDataGridJSON responseObject = new EasyUIDataGridJSON();
		responseObject.setTotal(ticketlist.size());
		responseObject.setRows(ticketlist);
		
		response.setContentType("application/json");
		response.getWriter().append(JSON.toJSONString(ticketlist));
		System.out.println(JSON.toJSONString(ticketlist));
	}
	
	//对密码进行修改
	public void doPasswordEdit(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession se = request.getSession();
		User user = (User) se.getAttribute("user");
		//User user = (User)request.getAttribute("user");
		//String username = request.getParameter("username");
		
		String pwd = user.getPassword();
		String oldpwd = request.getParameter("oldpwd");
		oldpwd = Md5Utils.md5(oldpwd);
		String newpwd = request.getParameter("newpwd");
		String newpwd2 = request.getParameter("newpwd2");
		System.out.println(pwd);
		System.out.println(oldpwd);
//		System.out.println(newpwd);
//		System.out.println(newpwd2);
		
		//User use = new User();
		if(!pwd.equals(oldpwd)) {
			//request.getRequestDispatcher("pwdedit.jsp").forward(request, response);
			request.setAttribute("msg", "password is wrong!");
			request.getRequestDispatcher("/error.jsp").forward(request,
					response);
		}
		else if(!newpwd.equals(newpwd2)) {
			//request.getRequestDispatcher("pwdedit.jsp").forward(request, response);
			request.setAttribute("msg", "confirm your password!");
			request.getRequestDispatcher("/error.jsp").forward(request,
					response);
		}
		else {
			UserDaoImpl udi=new UserDaoImpl();
//			User nuser = new User();
//			nuser.setPassword(newpwd2);
//			udi.modUserPwd(nuser);
			
			user.setPassword(Md5Utils.md5(newpwd2));
			udi.modUserPwd(user);
			
			request.setAttribute("msg", "modify success");
			request.getRequestDispatcher("/success.jsp").forward(request,
					response);
		}

	}
	
	
}
